package com.example.app.components


import android.util.Log
import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import com.example.app.R
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ListitemSample() {
    var list by remember {
        mutableStateOf(listOf<ListItemData>(
            ListItemData("一年一班", true),
            ListItemData("一年二班", false),
            ListItemData("一年三班", false),
            ListItemData("一年四班", false),
            ListItemData("一年五班", false),
            ListItemData("一年六班", false),
            ListItemData("一年六班", false),
            ListItemData("一年六班", false),
            ListItemData("一年六班", false),
            ListItemData("一年七班", false)
        ))
    }
    // 滚动
    val scrollState = rememberScrollState()
    // 携程域
    val coroutineScope = rememberCoroutineScope()

    // horizontalScroll横向滚动
    // verticalScroll竖向滚动
    Column(
        // verticalScroll使用这种方式滚动每一项不会销毁,一上来就全部初始化,如果列表数据很复杂很多可能会造成内存占用过高
        modifier = Modifier.verticalScroll(scrollState)
    ) {
        list.forEachIndexed { index, it ->
            // text主标题
            // secondaryText副标题
            // singleLineSecondaryText是否为单行文本
            // overlineText主标题上方文本
            // trailing右侧区域
            ListItem(
                icon = {
                    Icon(
                        imageVector = Icons.Default.AccountCircle,
                        contentDescription = ""
                    )
                },
                text = {
                    Text(text = "${it.title}")
                },
                secondaryText = {
                    Text(text = "副标题111111111111111111111111111111111111111111111111111111111111111")
                },
                singleLineSecondaryText = false,
                overlineText = {
                    Text(text = "主标题上方文本")
                },
                trailing = {
//                    Image(painter = painterResource(id = R.drawable.a), contentDescription = "")
//                    Icon(imageVector = Icons.Default.AccountBox, contentDescription = "")
                    Checkbox(checked = it.checked, onCheckedChange = {
                        list = list.mapIndexed { indexs, d ->
                            var newItem = d.copy()
                            if (index == indexs) {
                                // 直接改动属性是不会引起mutableStateOf的重组,效果不会改变,需要创建新的对象
                                newItem.checked = !d.checked
                            } else {
                                newItem.checked
                            }
                            newItem
                        }
                    })
                },
                modifier = Modifier.clickable {
                    // 滚动到滚动条最大位置
                    // scrollTo会阻碍主线程,需要携程域
                    coroutineScope.launch {
                        // scrollTo从初始值跳转到某一个位置
                        // scrollBy从当前位置跳转到某一个位置
                        // 前面带animate的是有动画
                        scrollState.animateScrollTo(scrollState.maxValue)
                    }
                }
            )
            DisposableEffect(Unit) {
                // 每一项初始化时执行
                Log.d("====", "effect: $it")
                onDispose {
                    // 每一项被销毁时执行
                    Log.d("====", "onDispose: $it")
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterialApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class)
@Composable
fun ListitemSample2() {
    // lazy滚动实例
    val lazyScroll = rememberLazyListState()
    // 携程域
    val coroutineScope = rememberCoroutineScope()
    val datas = listOf<Int>(1,2,3,4,5,6,7,9,1,1,1,1,11,1,1,1,1,1,1,1,1,1,1)
    // LazyColumn有缓存机制,只初始化当前显示的数据,以外的会销毁,可以节省性能
    LazyColumn(state = lazyScroll) {
        // 此处不能直接放组件
        // 单个组件需要使用item
        item {
//            Text(text = "column", modifier = Modifier.clickable {
//                coroutineScope.launch {
//                    // animateScrollToItem可以直接滚动至item的下标
//                    lazyScroll.animateScrollToItem(datas.size - 1)
//                }
//            })
        }
        // stickyHeader使消息固定,不跟随列表滚动
        stickyHeader {
            Text(text = "浮动消息")
        }
        // 多个组件
        items(datas) {
            ListItem(
                icon = {
                    Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
                },
                text = {
                    Text(text = "大标题")
                },
                modifier = Modifier.clickable {
                    coroutineScope.launch {
                        // animateScrollToItem可以直接滚动至item的下标
                        lazyScroll.animateScrollToItem(datas.size - 1)
                    }
                }
            )
        }
    }
}

@Preview
@Composable
fun ListitemSample2Preview() {

}

data class ListItemData(var title: String, var checked: Boolean)

@Preview(
    showSystemUi = true
)
@Composable
fun ListitemSamplePreview() {
    ListitemSample()
}

